package com.note.simpleshop.module.goods.service;
import com.note.simpleshop.entity.TbColumn;
import com.note.simpleshop.entity.TbGoods;
import com.note.simpleshop.module.goods.dao.ColumnRepository;
import com.note.simpleshop.module.goods.dao.GoodsRepository;
import com.note.simpleshop.module.goods.vo.ColumnVo;
import com.note.simpleshop.module.goods.vo.GoodsVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class ColumnService {
    private static final String CACHE_NAME="column_cache";
    private static final Logger logger = LoggerFactory.getLogger(ColumnService.class);
    @Autowired
    private ColumnRepository columnRepository;
    @Autowired
    private GoodsRepository goodsRepository;
    public List<TbColumn> findByIsDeleteOrderBySortAsc(Integer isdelete)
    {
        logger.info("****************findByIsDeleteOrderBySortAsc*************");
        return columnRepository.findByIsDeleteOrderBySortAsc(TbColumn.IS_DELEETE0);
    }
    @Cacheable(CACHE_NAME)
    public List<ColumnVo> findAll()
    {
        List<ColumnVo> columnVoList=new ArrayList<ColumnVo>();
        Map<String,ColumnVo> columnMap=new HashMap<String,ColumnVo>();
        List<TbColumn> colomuns=this.findByIsDeleteOrderBySortAsc(TbColumn.IS_DELEETE0);
        List<String> ids=new ArrayList<String>();
        for(TbColumn model : colomuns)
        {
            ids.add(model.getId());
            ColumnVo columnVo=ColumnVo.copy(model);
            columnMap.put(model.getId(),columnVo);
            columnVoList.add(columnVo);
        }
        List<TbGoods> goodslist=goodsRepository.findBySaleAndColumnIdInAndIsDelete(TbGoods.SALE1,ids,TbGoods.IS_DELEETE0);
        for(TbGoods model:goodslist)
        {
            ColumnVo columnVo=columnMap.get(model.getColumnId());
            columnVo.getGoodsList().add(GoodsVo.copy(model));
        }
        return columnVoList;
    }
}
